package com.spingbootes.kizuki.service;

import com.spingbootes.kizuki.dao.ESDao;
import com.spingbootes.kizuki.es.ESConfig;
import com.spingbootes.kizuki.es.ESManger;
import com.spingbootes.kizuki.es.ExecuteUtil;
import com.spingbootes.kizuki.es.IndexInfo;
import com.spingbootes.kizuki.pojo.ApexInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ESServiceImpl implements ESService{

    @Autowired
    private ESDao esDao;

    @Autowired
    private ExecuteUtil executeUtil;

    @Override
    public void insert(ApexInfo info) {
        int year = Integer.parseInt(getIndexYear(info.getId()));
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(year).endYear(year).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.insert(i, info)));
        ESManger<Integer> manger = new ESManger<>(executeUtil);
        manger.handler(map);
    }

    @Override
    public void delete(String id) {
        int year = Integer.parseInt(getIndexYear(id));
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(year).endYear(year).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.delete(i, id)));
        ESManger<Integer> manger = new ESManger<>(executeUtil);
        manger.handler(map);
    }

    @Override
    public void update(ApexInfo info) {
        int year = Integer.parseInt(getIndexYear(info.getId()));
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(year).endYear(year).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.update(i, info)));
        ESManger<Integer> manger = new ESManger<>(executeUtil);
        manger.handler(map);
    }

    @Override
    public ApexInfo select(String id) {
        int year = Integer.parseInt(getIndexYear(id));
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(year).endYear(year).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.select(i, id)));
        ESManger<ApexInfo> manger = new ESManger<>(executeUtil);
        return (ApexInfo) manger.handler(map).get(0);
    }

    @Override
    public List<ApexInfo> selectAll() {
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(2007).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.selectAll(i)));
        ESManger<List<ApexInfo>> manger = new ESManger<>(executeUtil);
        return manger.handler(map);
    }

    @Override
    public List<ApexInfo> selectDetail(String detailId) {
        IndexInfo indexInfo = IndexInfo.builder().indexPrefix(ESConfig.INDEX_PREFIX).beginYear(2010).build();
        List<String> indexs = indexInfo.getIndexs();
        Map<String, ExecuteUtil.Caller> map = new HashMap<>();
        indexs.forEach(i -> map.put(i, () -> esDao.selectDetail(i, detailId)));
        ESManger<List<ApexInfo>> manger = new ESManger<>(executeUtil);
        return manger.handler(map);
    }

    private String getIndexYear(String id){
        return id.substring(0, 4);
    }
}
